/**
 * localStorage.js
 * @author zhaoli
 */
const ls = window.localStorage

function get(key: string): any {
  let item = null;
  try {
    item = JSON.parse(ls.getItem(key))
  } catch {
    item = ls.getItem(key)
  }
  return item
}

function set(key: string, value: any): boolean {
  try {
    ls.setItem(key, JSON.stringify(value))
    return true
  } catch (e) {
    return false
  }
}

function remove(key: string | string[]): void {
  if (typeof key === 'string') {
    ls.removeItem(key)
  } else {
    key.map(a => {
      ls.removeItem(a)
    })
  }
}

function clear(): void {
  return ls.clear()
}

export default {
  set,
  get,
  remove,
  clear,
}
